package com.lazy.localfs;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.google.protobuf.UnknownFieldSet.Field;


public class DirTest {

    /**
     * 
     * @param index form 1 to 99999
     * @return
     */
    public static String getDirName(int index) {
        String prefix = "gps";
        return String.format("%s%05d", prefix, index);
    }
    
    public static void createDir() {
        for (int i = 0; i < 100; i++) {
            String dirName = getDirName(i);
            File dir = new File("data/" + dirName);
            if (!dir.exists()) {
                dir.mkdir();
            }
        }
    }
    
    public static List<File> getDataDir() {
        File pDir = new File("data");
        File[] files = pDir.listFiles();
        List<File> fileList = new ArrayList<File>();
        for (File file:files) {
            if (file.isDirectory()) {
                fileList.add(file);
            }
        }
        Collections.sort(fileList, new Comparator<File>(){
            @Override
            public int compare(File o1, File o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        
        return fileList;
    }
    
    public static void removeDir(List<File> list) {
        File file = list.remove(0);
        file.delete();
    }
    
    public static int getLastFile(List<File> list) {
        File file = list.get(list.size() - 1);
        String name = file.getName();
        String indexStr = name.substring(3);
        System.out.println(indexStr);
        
        return Integer.valueOf(indexStr);
    }
    
    public static void main(String[] args){
//        System.out.println(getDirName(1));
//        
//        for (File file:getDataDir()) {
//            System.out.println(file.getName());
//        }
//        
//        List fileList = getDataDir();
//        while (fileList.size() > 10) {
//            removeDir(fileList);
//        }
//        
//        System.out.println(getLastFile(fileList));
        
        File file1 = new File("hello");
        File file2 = new File("hello");
        
        System.out.println("is equal:" + (file1==file2));
        
    }
}
